package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;

import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;


    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(page,rows);
        Page<Supplier> page2 = supplierDao.allSupplier(page,rows,supplierName);
        map.put("total",page2.getTotal());
        map.put("rows",page2.getResult());
        return map;
    }

    @Override
    public void add(Supplier supplier) {
        if (supplier.getSupplierId()==null){
            supplierDao.add(supplier);
        }else{
            supplierDao.update(supplier);
        }

    }



    @Override
    public void delete(String ids) {
        //ids   "1,3,6,7"    idArray  [1,3,6,7]
        String[] idArray = ids.split(",");
        /*if (a.length>0){
            for (String id : a) {
                supplierDao.delete(id);
            }
        }*/
        supplierDao.delete(idArray);
    }


}
